<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-05-16 14:47:31
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-24 14:28:08
-->
<template>
  <div>
    <el-dialog
      title="定向发放"
      :visible.sync="dialog.dirShow"
      width="50%"
      @close="dirClose"
      custom-class="overviewClass"
    >
      <el-form
        :model="grantForm"
        ref="grantForm"
        :rules="grantRules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="选择用户" prop="type">
          <el-radio-group v-model="grantForm.type" @change="raChange">
            <el-radio label="1">全部用户</el-radio>
            <el-radio label="2">企业用户</el-radio>
            <el-radio label="5">个人用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <div class="add" @click="addUser" v-if="grantForm.type != 1">
          + 添加用户
        </div>
        <div v-if="grantForm.type == 2">
          <el-table :data="enterData">
            <el-table-column label="用户ID" prop="cSerialNumber"> </el-table-column>
            <el-table-column label="用户昵称" prop="cName"> </el-table-column>
            <el-table-column label="手机号码" prop="phone"> </el-table-column>
            <el-table-column label="关联时间" prop="createTime">
            </el-table-column>
          </el-table>
        </div>
        <div v-if="grantForm.type == 5">
          <el-table :data="perData">
            <el-table-column label="用户ID" prop="pSerialNumber"> </el-table-column>
            <el-table-column label="用户昵称" prop="pName"> </el-table-column>
            <el-table-column label="手机号码" prop="phone"> </el-table-column>
            <el-table-column label="关联时间" prop="createTime">
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <span slot="footer">
        <el-button @click="dialog.dirShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog title="关联企业用户" :visible.sync="enterpriseShow" width="50%">
      <el-table :data="enterpriseData" @selection-change="enterSelectionChange">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column label="用户ID" prop="cId"> </el-table-column>
        <el-table-column label="用户昵称" prop="cName"> </el-table-column>
        <el-table-column label="手机号码" prop="phone"> </el-table-column>
        <el-table-column label="关联时间" prop="createTime"> </el-table-column>
      </el-table>
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        background
        :page-size="spQuery.pageSize"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        @size-change="spSizeChange"
        @current-change="spCurrentChange"
        :current-page="spQuery.pageNum"
        layout=" ->,total,sizes, prev, pager, next, jumper "
        :total="spQuery.total"
      >
      </el-pagination>
      <span slot="footer">
        <el-button @click="enterpriseShow = false">取消</el-button>
        <el-button type="primary" @click="enterpriseSubmit">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog title="关联个人用户" :visible.sync="personalShow" width="50%">
      <el-table :data="personalData" @selection-change="perSelectionChange">
        <el-table-column type="selection"> </el-table-column>
        <el-table-column label="用户ID" prop="pId"> </el-table-column>
        <el-table-column label="用户昵称" prop="pName"> </el-table-column>
        <el-table-column label="手机号码" prop="phone"> </el-table-column>
        <el-table-column label="关联时间" prop="createTime"> </el-table-column>
      </el-table>
      <el-pagination
        prev-text="上一页"
        next-text="下一页"
        background
        :page-size="posalQuery.pageSize"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        @size-change="poSizeChange"
        @current-change="poCurrentChange"
        :current-page="posalQuery.pageNum"
        layout=" ->,total,sizes, prev, pager, next, jumper "
        :total="posalQuery.total"
      >
      </el-pagination>
      <span slot="footer">
        <el-button @click="personalShow = false">取消</el-button>
        <el-button type="primary" @click="personalSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { showSpRelateC, posalList, grant } from "@/api/order/coupon";
export default {
  props: {
    dialog: {
      type: Object,
      required: true,
    },
    drawerData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      enterData: [],
      perData: [],
      spQuery: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      posalQuery: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      enterpriseData: [],
      personalData: [],
      enterpriseShow: false,
      personalShow: false,
      grantForm: {
        couponId: this.drawerData.id,
        type: "1",
        orgIds: [],
        userIds: [],
      },
      grantRules: {
        type: [
          {
            required: true,
            message: "请选择发放类型",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    raChange() {
      if (this.grantForm.type == 1) {
        this.grantForm.orgIds = [];
        this.grantForm.userIds = [];
        this.perData = [];
        this.enterData = [];
      } else if (this.grantForm.type == 2) {
        this.grantForm.userIds = [];
        this.perData = [];
      } else if (this.grantForm.type == 5) {
        this.grantForm.orgIds = [];
        this.enterData = [];
      }
    },
    getSp() {
      showSpRelateC({
        pageNum: this.spQuery.pageNum,
        pageSize: this.spQuery.pageSize,
      }).then((res) => {
        this.enterpriseData = res.rows;
      });
    },
    getPosal() {
      posalList({
        pageNum: this.posalQuery.pageNum,
        pageSize: this.posalQuery.pageSize,
      }).then((res) => {
        this.personalData = res.rows;
      });
    },
    // 添加用户
    addUser() {
      if (this.grantForm.type == 2) {
        this.enterpriseShow = true;
        this.getSp();
      } else if (this.grantForm.type == 5) {
        this.personalShow = true;
        this.getPosal();
      }
    },
    // 个人保存
    personalSubmit() {
      this.personalShow = false;
    },
    // 企业保存
    enterpriseSubmit() {
      this.enterpriseShow = false;
    },
    // 保存
    onSubmit() {
      if (this.grantForm.type == 2) {
        if (this.grantForm.orgIds.length == 0)
          return this.$message.info("请关联企业用户");
      } else if (this.grantForm.type == 5) {
        if (this.grantForm.userIds.length == 0)
          return this.$message.info("请关联个人用户");
      }
      grant(this.grantForm).then((res) => {
        this.$message.success(res.msg);
        this.$parent.getList();
        this.dialog.dirShow = false;
      });
    },
    // 关闭
    dirClose() {
      this.dialog.dirShowEject = false;
    },
    poSizeChange(val) {
      this.spQuery.pageSize = val;
      this.spQuery.pagenum = 1;
      this.getSp();
    },
    poCurrentChange(val) {
      this.spQuery.pagenum = val;
      this.getSp();
    },
    spSizeChange(val) {
      this.posalQuery.pageSize = val;
      this.posalQuery.pagenum = 1;
      this.getPosal();
    },
    spCurrentChange(val) {
      this.posalQuery.pagenum = val;
      this.getPosal();
    },
    enterSelectionChange(val) {
      this.enterData = val;
      val.forEach((item) => {
        this.grantForm.orgIds.push(item.cId);
      });
    },
    perSelectionChange(val) {
      this.perData = val;
      val.forEach((item) => {
        this.grantForm.userIds.push(item.pId);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.add {
  color: #1267fa;
  cursor: pointer;
}
.el-pagination {
  margin-top: 24px;
  /deep/ .el-pagination__total {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    margin-right: 25px;
  }
  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-input__inner {
    // width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .more {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-pagination__jump {
    .el-input__inner {
      width: 40px;
      height: 40px;
    }
  }
}
/deep/ .overviewClass {
  .el-dialog__body {
    height: 720px;
    overflow: auto;
  }
}
</style>